/**
 * Created by hcadmin on 16/5/16.
 */
define([
    "jquery",
    "IO",
    "UPLOADUI",
    "UPLOAD",
    "jqueryUI",
    "bootbox"
], function($,IO,jui,upui,up,bootbox,definebt) {
    $(function(){

        gettable();

        $(".selectpro").on("change",function(){
            //console.log($(this).val());
            document.reg_testdate.selectcity.innerHTML='请选择市';
            document.reg_testdate.selectcity.options.add(new Option("请选择市","-1"));

            document.reg_testdate.selectarea.innerHTML='请选择区';
            document.reg_testdate.selectarea.options.add(new Option("请选择区","-1"));

            var provinceList = JSON.parse($("#province").val());
            var obj;
            for (obj in provinceList){
                if(provinceList[obj].name == $(this).val()){
                    for (var x in provinceList[obj].cityList){
                        var city = provinceList[obj].cityList;
                        document.reg_testdate.selectcity.options.add(new Option(city[x].name,city[x].code));
                    }
                }
            }
        });

        $(".selectcity").on("change",function(){
            document.reg_testdate.selectarea.innerHTML='请选择区';
            document.reg_testdate.selectarea.options.add(new Option("请选择区","-1"));
            var province = $(".selectpro option:selected").val();
            var provinceList = JSON.parse($("#province").val());
            var obj;
            for (obj in provinceList){
                if(provinceList[obj].name == province){
                    var city = provinceList[obj].cityList;
                    for (var x in city){
                        if ($(".selectcity option:selected").text()==city[x].name){
                            var area = city[x].areaList;
                            for (var y in area){
                                document.reg_testdate.selectarea.options.add(new Option(area[y].name,area[y].code));
                                //console.log("城市们",area[y].name);
                            }

                        }

                    }
                }
            }
        });

        //新增公司
        $('.btnAdd').click(function(){
           var company_name = $("#company_name_add").val();
           var company_phone = $("#company_phone").val();
           var province_id = $(".selectpro option:selected").attr('data-id');
           var city_id = $(".selectcity option:selected").val();
           var area_id = $(".selectarea option:selected").val();
            var address=$("#address").val();
            var leader_name=$("#leader_name").val();
            var leader_tele=$("#leader_tele").val();
            var yuesao='';
            if(company_name.length<=0){
                bootbox.alert('请填写公司名称');
                return;
            }
            if(company_phone.length<=0){
                bootbox.alert('请填写公司电话');
                return;
            }
            if(address.length<=0){
                bootbox.alert('请填写公司地址');
                return;
            }
            if(leader_name.length<=0){
                bootbox.alert('请填写公司负责人姓名');
                return;
            }
            if(leader_tele.length<=0){
                bootbox.alert('请填写公司负责人联系电话');
                return;
            }
            if(province_id==-1){
                bootbox.alert('请选择省');
                return;
            }
            if(city_id=="-1"){
                bootbox.alert('请选择市');
                return;
            }
            if(area_id=="-1"){
                bootbox.alert('请选择区');
                return;
            }
            var params={
                company_name:company_name,
                company_phone:company_phone,
                address:address,
                leader_name:leader_name,
                leader_tele:leader_tele,
                province_id:province_id,
                city_id:city_id,
                area_id:area_id,
                yuesao:yuesao
            };
            IO.get("/api/homemaking_company", params, function (data) {
                    if (data.code == 0) {
                        bootbox.alert('添加成功');
                        $('#myModal').modal('hide');
                        gettable();
                    }
                     else {
                        bootbox.alert('新增公司失败');
                        $('#myModal').modal('hide');
                    }
            })
        });

        $('#myModal').on('hide.bs.modal', function () {
            $("#company_name_add").val("");
            $("#company_phone").val("");
            $("#address").val("");
            $("#leader_name").val("");
            $("#leader_tele").val("");
        })

        //根据公司查询
        $(".btnSeach").click(function(){
            gettable();
        })
    });

    function gettable(){

        var grid;
        //每列数据列性
        var columns = [
            {
                title: '序号',
                dataIndex: 'company_id',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].company_id + "</span>";

                }
            },
            {
                title: '名称',
                dataIndex: 'company_name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].company_name+ "</span>";
                }
            },
            {
                title: '服务项目',
                dataIndex: 'service',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].service + "</span>";

                }
            },
            {
                title: '负责人',
                dataIndex: 'typecount',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].leader_name + "</span>";

                }
            },
            {
                title: '联系方式',
                dataIndex: 'leader_tele',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].leader_tele + "</span>";

                }
            },
            {
                title: '操作',
                dataIndex: 'action',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<a href='#' company_id="+data[datarow].company_id+" class='detail'><span style='color: #6ab7ad;'>" + "查看详情" + "</span></a>";
                }
            }
        ];
        //数组初始化

        function dataInit() {

            grid = new Grid("/api/homemaking_company/list", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 10);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {
            var company_name=$("#company_name").val();
            //console.log(company_name)
            var param="company_name="+company_name;
            return param;
        }
        dataInit();
    }
    $(document).on('click','.detail',function(){
        var company_id=$(this).attr('company_id');
        location.href='/96053/company_detail?company_id='+company_id;
    });
});